<?php
#### TEST CONTROLLER ###########################################################
class TestController {

	#### CONSTRUCTOR ###########################################################
	function TestController() {
		Base::getThis();

		// Set the page title
		$this->page_title = _('Test Module');
		if (url(2)) $this->page_title .= ' '.ucwords(url(2));

		// The header is to be included module wide
		$this->buildHeader();

		// Perform default function
		invoke_method($this, url(2));
	}

	#### BUILD HEADER ##########################################################
	function buildHeader() {

		// To Exlude this header from being shown on a function set the functions output as
		// "$this->output =" rather than appending to $this->output with "$this->output .="

		// Build the links array
		$links[] = array(_('Home'), url(1));
		$links[] = array(_('Table'), url(1).'/table');
		$links[] = array(_('Login'), url(1).'/login');
		$links[] = array(_('Form'), url(1).'/form');
		$links[] = array(_('Database'), url(1).'/database');
		$links[] = array(_('Notice'), url(1).'/notice');
		$links[] = array(_('Image Popup'), url(1).'/image-viewer');

		// Include categories library, configure categories and return peformed action such as manage/edit/add
		$mod_head = $this->load->library('module_header');
		$mod_head->setName(_('Test Module'));
		$mod_head->setIcon($this->icon['about']);
		$mod_head->setLinks($links);
		$this->output = $mod_head->output();
	}

	#### MAIN ##################################################################
	function main() {

		$this->body = _('The test module serves as a demonstration of various helpers and libraries present within the framework.
			Its intended purpose it to provide a working example for developers to learn from.');

		$this->output .= box($this->icon['about'].' &lt;?php echo \'hello\'; ?>', $this->body);
	}

	#### TABLE #################################################################
	function table() {

		// Load table library
		$tbl = $this->load->library('table');

		// Create example table
		$tbl->open();
		$tbl->tr(
			$tbl->th(_('Column 1')).
			$tbl->th(_('Column 2'))
			);

		$tbl->tr(
			$tbl->td(_('Row 1')).
			$tbl->td(_('Row 1'))
			);
		$tbl->tr(
			$tbl->td(_('Row 2')).
			$tbl->td(_('Row 2'))
			);

		$td_array[] = $tbl->td(_('Grid 1'));
		$td_array[] = $tbl->td(_('Grid 2'));
		$td_array[] = $tbl->td(_('Grid 3'));
		$td_array[] = $tbl->td(_('Grid 4'));

		$tbl->tr_grid($td_array, 2);

		$tbl->tr(
			$tbl->td(_('Table Footer'), TRUE, 2)
			, NULL, 'footer');
		$tbl->close();

		$body = $tbl->output();

		$template = new Template('box', 'common');
		$template->assign('TITLE', _('Table Test'));
		$template->assign('BODY', $body);
		$this->output .= $template->output();
	}

	#### NOTICE ################################################################
	function notice() {

		$title = _('Notice Test');
		$body = _('Below is a list of all available notices generated from the <b>notice()</b> function.');

		$body .= notice(_('This is a <b>success</b> message.'));
		$body .= notice(_('This in an <b>info</b> message.'), 'info');
		$body .= notice(_('This is a <b>warning</b> message.'), 'warning');
		$body .= notice(_('This is an <b>error</b> message.'), 'error');

		$this->output .= box($title, $body);
	}

	#### IMAGE VIEWER ##########################################################
	function imageViewer() {

		$title = _('Image Viewer Test');

		// Load table library
		$tbl = $this->load->library('table');

		// Create example table
		$tbl->open(NULL, NULL, 'center');
		$td_array[] = $tbl->td('<a class="viewer" href="http://farm4.static.flickr.com/3524/3199024385_b6152f5f54.jpg" title="Mushroom Money by Riddian, on Flickr"><img src="http://farm4.static.flickr.com/3524/3199024385_b6152f5f54_m.jpg" width="240" height="160" alt="Mushroom Money" /></a>', TRUE);
		$td_array[] = $tbl->td('<a class="viewer" href="http://farm4.static.flickr.com/3424/3199015891_4130639a3a.jpg" title="Icy Branch by Riddian, on Flickr"><img src="http://farm4.static.flickr.com/3424/3199015891_4130639a3a_m.jpg" width="240" height="160" alt="Icy Branch" /></a>', TRUE);
		$td_array[] = $tbl->td('<a class="viewer" href="http://farm3.static.flickr.com/2145/2529151352_767149001a.jpg" title="Looking up to a building by Riddian, on Flickr"><img src="http://farm3.static.flickr.com/2145/2529151352_767149001a_m.jpg" width="240" height="160" alt="Looking up to a building" /></a>', TRUE);
		$td_array[] = $tbl->td('<a class="viewer" href="http://farm3.static.flickr.com/2141/2583982304_8001146f49.jpg" title="Red Flowers by Riddian, on Flickr"><img src="http://farm3.static.flickr.com/2141/2583982304_8001146f49_m.jpg" width="240" height="160" alt="Red Flower" /></a>', TRUE);
		$td_array[] = $tbl->td('<a class="viewer" href="http://farm4.static.flickr.com/3126/2583144067_beaf2413df.jpg" title="Yellow Flower by Riddian, on Flickr"><img src="http://farm4.static.flickr.com/3126/2583144067_beaf2413df_m.jpg" width="240" height="160" alt="Yellow Flower" /></a>', TRUE);
		$td_array[] = $tbl->td('<a class="viewer" href="http://farm4.static.flickr.com/3048/2977316097_f2ec97cc8f.jpg" title="Humber Hawk by Riddian, on Flickr"><img src="http://farm4.static.flickr.com/3048/2977316097_f2ec97cc8f_m.jpg" width="240" height="160" alt="Humber Hawk" /></a>', TRUE);
		$tbl->tr_grid($td_array, 2);
		$tbl->close();

		// Typically these lines wouldn't be included if you want class viewer detection sitewide
		$popup = $this->load->library('popup');
		$body = $popup->output();

		$body .= $tbl->output();
		$this->output .= box($title, $body);
	}

	#### FORM ##################################################################
	function form() {

		$body = '<h3>'._('Posted Form Variables').':</h3>'.($this->post ? '<div style="width:100%;overflow:auto">'.print_array($this->post).'</div>': _('Form has not been posted').'<br/>');

//		$this->post['datetime'] = date_to_datetime($this->post['hour'], $this->post['minute'], $this->post['day'], $this->post['month'], $this->post['year']);

		// Load form library
		$form = $this->load->library('form');
		$form->open();
		$form->openFieldset(_('Fieldset'));
		$form->text(_('Text Box').'*', 'text_box', @$this->post['text_box']);
		$form->date(_('Date & Time'), @$this->post['datetime']);
		$form->openGroup(_('Grouped Form Item'));
		$form->file(NULL, 'file[upload]');
		$form->closeGroup();
		$form->texteditor(_('Text Editor'), 'text_editor', @$this->post['text_editor'], 100, 100);
		$form->checkbox(_('Checkbox'), 'checkbox', @$this->post['checkbox'], 0);
		$form->button('submit', _('Submit Button'));
		$form->closeFieldset();
		$form->close();
		$body .= $form->output();

		$template = new Template('box', 'common');
		$template->assign('TITLE', _('Form Test'));
		$template->assign('BODY', $body);
		$this->output .= $template->output();
	}

	#### DATABASE ##################################################################
	function database() {

		$this->body = '
			<a href="'.url(1, 2).'">'._('Home').'</a> '.$this->icon['separator'].'
			<a href="'.url(1, 2).'/database-cache">'._('Database Cache').'</a>';

		// Perform default function
		$action = clean_method_url(url(3));
		if(method_exists($this, $action)) $this->{$action}();

		// Create example query
		$this->db->select($this->db->table['common']);
		$this->db->query() or sql_error(__FILE__, __LINE__);
		$results = $this->db->result();

		$output = NULL;
		if ($results) foreach ($results as $result) {
			$output .= '&nbsp;&nbsp;&nbsp;&nbsp;'.$result['name'].' '.$result['value'].'<br/>';
		}

		$this->body .= '<br /><br /><h3>'._('Listing data from').' SELECT * FROM config</h3>'.$output;

		$template = new Template('box', 'common');
		$template->assign('TITLE', _('Database Test'));
		$template->assign('BODY', $this->body);
		$this->output .= $template->output();
	}


	#### POPUP LOGIN ###########################################################
	function popupLogin() {
		$this->load->helper('login');
		popupLoginForm(url(1, 2).'/popup-login', url(1).'/login', url(1).'/logout');
	}

	#### LOGINTEST #############################################################
	function login() {

		$this->body = '
			<a href="'.url(1, 2).'">'._('Home').'</a> '.$this->icon['separator'].'
			<a href="'.url(1, 2).'/create-session">'._('Create Session').'</a> '.$this->icon['separator'].'
			<a href="'.url(1, 2).'/automatic-login">'._('Automatic Login').'</a> '.$this->icon['separator'].'
			<a href="'.url(1, 2).'/manual-login">'._('Manual Login Form').'</a> '.$this->icon['separator'].'
			<a id="popup-login-test" href="'.url(1, 2).'/manual-login">'._('Popup Login Form').'</a> '.$this->icon['separator'].'
			<a href="'.url(1, 2).'/logout">'._('Logout').'</a>';

		// Perform default function
		$action = clean_method_url(url(3));
		if(method_exists($this, $action)) $this->{$action}();

		$this->body .= '<br/><br/>'._('You are currently').': <b>'.($this->sessvar['login'] ? _('Logged In') : _('Logged Out')).'
			</b><br/><br/><h3>'._('Session Array').'</h3>'.($this->sessvar ? '<div style="width:100%;overflow:auto">'.print_array($this->sessvar, TRUE).'</div>' : _('Session does not exist'));

		// Popup Login
		$popup = $this->load->library('popup');
		$popup->width('400px');
		$popup->onClickElement('#popup-login-test');
		$popup->url(url(1, 2).'/popup-login');
		$this->body .= $popup->output();

		$template = new Template('box', 'common');
		$template->assign('TITLE', _('Login Test'));
		$template->assign('BODY', $this->body);
		$this->output .= $template->output();

	}

	#### CREATE SESSION ########################################################
	function createSession() {
		$this->session->user = array('user_id' => 1,
			'username' => 'Test',
			'last_login_datetime' => datetime(),
			'last_login_ip' => $_SERVER['REMOTE_ADDR'],
			'cookie_id' => md5('test'),
			'session_ip' => '000.000.000.000');
		$this->session->create();
	}

	#### MANUAL LOGIN ##########################################################
	function manualLogin() {

		$this->body .= '<br/><br/>';
		if ($this->post['username']) $this->body .= _('Trying to login with username').': <b>'.$this->post['username'].'</b><br/>';
		if ($this->post['password']) $this->body .= _('Trying to login with password').': <b>'.$this->post['password'].'</b><br/>';

		$form = $this->load->library('form');
		$form->open();
		$form->openFieldset();
		$form->text(_('Username'), 'username');
		$form->password(_('Password'), 'password');
		$form->checkbox(_('Remember Login'), 'remember');
		$form->button('submit', _('Submit'));
		$form->closeFieldset();
		$form->close();
		$this->body .= $form->output();

		if ($this->post['username'] && $this->post['password']) {
			$this->session->username = $this->post['username'];
			$this->session->password = $this->post['password'];
			$this->session->remember = $this->post['remember'];
			if ($this->session->login()) $this->body .= '<br/>'._('Login Status').': <b>'._('Success').'</b>';
			else $this->body .= '<br/>'._('Login Status').': <b>'._('Failed').'</b>';
		}
	}

	#### LOGIN #################################################################
	function automaticLogin() {
		$this->session->username = 'developer';
		$this->session->password = 'test';
		$this->session->login();
	}

	#### LOGOUT ################################################################
	function logout() {
		$this->session->logout();
		echo redirect($this->config['url'].url(1).'/login', TRUE);
	}

	#### CACHE #################################################################
	function databaseCache() {

		// Load caching functions
		$this->load->helper('cache');

		if (check_cache('test')) $cached = '<b>'._('Cached').'</b>';
		else $cached = '<b>'._('Not Cached').'</b>';

		$this->page_title .= ' ('._('Database').')';

		// Create example query
		$this->db->select($this->db->table['common']);
		$results = $this->db->cache('test');

		$output = NULL;
		if ($results) foreach ($results as $result) {
			$output .= '&nbsp;&nbsp;&nbsp;&nbsp;'.$result['name'].' '.$result['value'].'<br/>';
		}

		$output .= '<br />This query is: '.$cached.'<br />';
		$output .= '<br /><a href="'.url(1, 1).'/databasecache">'._('Refresh Page?').'</a>';
		$output .= '<br /><a href="'.url(1, 1).'/deletedatabasecache">'._('Delete Cache?').'</a>';

		$body = '<h3>'._('Lising data from').' SELECT * FROM config</h3>'.$output;

		$template = new Template('box', 'common');
		$template->assign('TITLE', _('Database Test'));
		$template->assign('BODY', $body);
		$this->output .= $template->output();

	}

	#### DELETE CACHE ##########################################################
	function deleteDatabaseCache() {
		// Load DB caching functions
		$this->load->helper('cache');

		delete_cache('test');
		$this->databaseCache();
	}
}
?>